/**
 * @desc contact form
 */
jQuery(function ($) {

    var form_contact = true;

    $('form#form-contact a.submit-button').click(function () {
        if (sessvars.n0aX.contact_sent === undefined) sessvars.n0aX.contact_sent = 0;

        if (form_contact) {

            var _data = $('form#form-contact').serialize();
            $.ajax({
                url: _root + 'contact/?' + _ajaxext,
                data: _data,
                type: 'post',
                dataType: 'json',
                beforeSend: function () {
                    $('div#result.success,div#result.error').hide();
                    $('form#form-contact li:last-child').fadeOut();
                    form_contact = false;
                },
                success: function (json) {

                    if (!$.isPlainObject(json)) {

                        var json = $.parseJSON('{"status":"0","header":"Hata","message":"Sunucu Hatası."}');
                        $('div#result').removeClass('success').addClass('error').show().html('<div class="bold">' + json.header + '</div>' + json.message);
                        $('form#form-contact li:last-child').fadeIn();
                        form_contact = true;
                        return false;
                    }

                    if (json.status == 1) {
                        $('div#result').removeClass('error').addClass('success').show().html('<div class="bold">' + json.header + '</div>' + json.message);
                        $('form#form-contact li:not(:has(>div#result))').fadeOut();
                        sessvars.n0aX.contact_sent++;
                    } else {
                        $('div#result').removeClass('success').addClass('error').show().html('<div class="bold">' + json.header + '</div>' + json.message);
                        $('form#form-contact li:last-child').fadeIn();
                        form_contact = true;

                    }
                }
            });
        }
        return false;
    });
    /**
     * @desc login/register
     */

    var _formregister = $('#wrapper form#form-register');

    var _inputs = _formregister.find('input:text');

    _inputs.bind('focus', function () {
        $(this).removeClass('fielderror');
    });

    $('#wrapper form.forms-login a.submit').live('click', function () {
        var _this = $(this);
        if(_this.hasClass('clicked')) { return false; }
        _this.toggleClass('clicked').hide();
        var _formid = _this.data('formid');
        var _action = _this.data('action');
        var _thisform = $('form#' + _formid);
        var _data = _thisform.serialize();
        
        /*
            var _error = $.psys_checkform(_thisform);
            if(_error) { return false; }
		*/

        $.ajax({
            url: _root + 'ajax/user/?act=' + _action + _ajaxext,
            type: 'post',
            cache: false,
            dataType: 'json',
            data: _data,
            beforeSend: function () {
            },
            success: function (json) {
                if (json.xhr_status == 1) {
                    $("#modal_message").attr('title', json.header).html(json.message);
                    openModal();
                    if (_formid == 'form-login') {
                        $.doTimeout(2000, function () {
                            document.location.href = _root + 'admin/tr-index.html';
                        });
                    }
                    if (_formid == 'form-userinfo') {
					$.doTimeout(1000, function () {
                            document.location.reload();
                        });
                    }					
                } else {
                	if(_action == "login")
                		{
                    $("#modal_message").attr('title', json.header).html(json.message+'<br /><br />'+$("#modal_login_helper").html());
                		}
                	else
                		{
                		$("#modal_message").attr('title', json.header).html(json.message);
                		}
                    openModal();
                    _this.toggleClass('clicked').show();
                }
            }
        });

        return false;
    });

    /* register */
    
    $('#wrapper #loginbox a.toggler').bind('click',function(){
    	var _this = $(this);
    	$('#wrapper #loginbox #'+_this.data('parent')).slideUp(function(){
    		$('#wrapper #loginbox #'+_this.data('target')).slideDown();
    	});
    });
    
    $('#wrapper #login-area .toggler').bind('click',function(){
        var _this = $(this);
        $('#'+_this.data('parent')).slideUp(function(){
            $('#'+_this.data('target')).slideDown();
        });
        return false;

    });    

});